package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.RolePersonBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.RoleBiz;
import com.mentaleye.biz.RolePersonBiz;
import com.mentaleye.biz.StudentsBiz;
import com.mentaleye.biz.TeacherBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping("/rolePerson")
public class RolePersonController {
	@Autowired
	private RoleBiz roleBiz;
	@Autowired
	private RolePersonBiz rolePersonBiz;
	@Autowired private TeacherBiz teacherBiz;
	
	@Autowired private StudentsBiz studentsBiz;
	@Autowired
	private DictionaryBiz dictionaryBiz;
	//教师列表
	@RequestMapping("/rolePersonList")
	public ModelAndView list(@RequestParam(value = "roleNo", required = false) Integer id) {
		ModelAndView view = new ModelAndView("ss/role/rolePersonList");
		Map<Integer, String> personType = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.PSERSON_TYPE.getCode());
		view.addObject("role", roleBiz.getRoleByNo(id));
		view.addObject("personType", JSONObject.toJSONString(personType));
		view.addObject("roleNo", id);
		return view;
	}
	//教师列表数据
	@RequestMapping("/rolePersonListData")
	@ResponseBody
	public String roleListData(CommonPage commonPage) {
		rolePersonBiz.queryRolePersonByPage(commonPage);
		return commonPage.toSHowPage();
	}
	//关联教师添加
	@RequestMapping("/rolePersonEdit")
	public ModelAndView edit(@RequestParam(value = "roleNo", required = false) Integer id){
		ModelAndView view = new ModelAndView("ss/role/rolePersonEdit");
		view.addObject("roleNo", id);
		view.addObject("teacherList", teacherBiz.queryAllTeacherByRoleNo(id));
		return view;
	}
	//关联教师添加
		@RequestMapping("/roleStudentEdit")
		public ModelAndView editRoleStudent(@RequestParam(value = "roleNo", required = false) Integer id){
			ModelAndView view = new ModelAndView("ss/role/roleStudentEdit");
			view.addObject("roleNo", id);
			view.addObject("studentList", studentsBiz.getStuByRoleNo(id));
			return view;
		}
	
	//保存角色人员关联
	@RequestMapping("/save")
	public @ResponseBody String save(RolePersonBean rolePersonBean,int[] personNo) {
		if(personNo !=null && personNo.length>1){
			List<RolePersonBean> rolePersonBeanList = new ArrayList<RolePersonBean>();
			for(int i = 0;i<personNo.length;i++){
				RolePersonBean rolePerson = new RolePersonBean();
				rolePerson.setRoleNo(rolePersonBean.getRoleNo());
				rolePerson.setPersonNo(personNo[i]);
				rolePerson.setPersonType(rolePersonBean.getPersonType());
				rolePersonBeanList.add(rolePerson);
			}
			return JSONObject.toJSONString(rolePersonBiz.addRolePersonList(rolePersonBeanList));
		}
		return JSONObject.toJSONString(rolePersonBiz.addRolePerson(rolePersonBean));
	}
	//删除角色人员关联，删除单条
	@RequestMapping("/deleteSingle")
	public @ResponseBody String deleteSingle(@RequestParam(value = "personId", required = false) Integer id,@RequestParam(value = "roleNo",required=false) Integer roleNo,@RequestParam(value = "personType",required=false) Integer personType) {
		return JSONObject.toJSONString(rolePersonBiz.dropRolePersonByPersonNo(id,roleNo,personType));
	}
	//删除多条
	@RequestMapping("/delete")
	public @ResponseBody String delete(@RequestParam(value = "param", required = false) int[] ids,@RequestParam(value = "roleNo",required=false) Integer roleNo) {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < ids.length; i++) {
			int j = ids[i];
			list.add(j);
		}
		for (Integer integer : list) {
			System.out.println(integer);
		}
		
		return JSONObject.toJSONString(rolePersonBiz.dropRolePersonByPersonList(list,roleNo));
	}
	
	

}
